package 抢红包;

import java.util.Random;

public class RedWarsThread{
    private static int sumMoney=50;

    public synchronized static void show(){
          if (sumMoney>0) {
              Random r = new Random();
              int money = r.nextInt(11);
              System.out.println(Thread.currentThread().getName() + "抢到了" + money + "块钱");
              sumMoney=sumMoney-money;
              /*if ((sumMoney-money)<=0){
                  System.out.println(Thread.currentThread().getName()+"抢到了" + sumMoney + "块钱");
              }*/
          }

    }


    public static void main(String[] args) {
        for (int i = 1; i <=5 ; i++) {
                new Thread(() -> {
                        while (true) {
                            show();
                            try {
                                Thread.sleep(300);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            if (sumMoney<=0){
                                System.out.println("没钱了");
                                break;
                            }
                        }

                }, i + "号选手").start();
            }


    }
}
